package cn.edu.sdjzu.xg.eagle.mapper;

import cn.edu.sdjzu.xg.eagle.entities.File;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface FileMapper {

    // 动态添加文件
    @Insert({"<script>",
            "INSERT INTO File ",
            "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">",
            "<if test=\"name != null\">name,</if>",
            "<if test=\"no != null\">no,</if>",
            "<if test=\"remarks != null\">remarks,</if>",
            "<if test=\"school.id != null\">sid,</if>",
            "</trim>",
            "VALUES ",
            "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">",
            "<if test=\"name != null\">#{name},</if>",
            "<if test=\"no != null\">#{no},</if>",
            "<if test=\"remarks != null\">#{remarks},</if>",
            "<if test=\"school.id != null\">#{school.id},</if>",
            "</trim>",
            "</script>"})
    boolean addFile(File file);

    // 动态删除文件
    @Delete("DELETE FROM File WHERE id = #{id}")
    boolean deleteFile(Integer id);

    // 动态修改文件
    @Update({"<script>",
            "UPDATE File ",
            "<set>",
            "<if test=\"name != null\">name = #{name},</if>",
            "<if test=\"no != null\">no = #{no},</if>",
            "<if test=\"remarks != null\">remarks = #{remarks},</if>",
            "<if test=\"school.id != null\">sid = #{school.id},</if>",
            "</set>",
            "WHERE id = #{id}",
            "</script>"})
    boolean updateFile(File file);

    // 动态查询文件
    @Select({"<script>",
            "SELECT ",
            "c.id AS File_id, c.name AS File_name, c.no AS File_no, c.remarks AS File_remarks, ",
            "s.id AS school_id, s.name AS school_name, s.no AS school_no, s.remarks AS school_remarks ",
            "FROM File c ",
            "JOIN School s ON c.sid = s.id ",
            "<where>",
            "<if test=\"id != null\">c.id = #{id}</if>",
            "<if test=\"name != null\">AND c.name = #{name}</if>",
            "<if test=\"no != null\">AND c.no = #{no}</if>",
            "</where>",
            "</script>"})
    @Results({
            @Result(property = "id", column = "File_id"),
            @Result(property = "name", column = "File_name"),
            @Result(property = "no", column = "File_no"),
            @Result(property = "remarks", column = "File_remarks"),
            @Result(property = "school.id", column = "school_id"),
            @Result(property = "school.name", column = "school_name"),
            @Result(property = "school.no", column = "school_no"),
            @Result(property = "school.remarks", column = "school_remarks")
    })
    List<File> findFiles(Map<String, Object> params);

    // 根据 ID 查询文件
    @Select("SELECT " +
            "c.id AS File_id, c.name AS File_name, c.no AS File_no, c.remarks AS File_remarks, " +
            "s.id AS school_id, s.name AS school_name, s.no AS school_no, s.remarks AS school_remarks " +
            "FROM File c " +
            "JOIN School s ON c.sid = s.id " +
            "WHERE c.id = #{id}")
    @Results({
            @Result(property = "id", column = "File_id"),
            @Result(property = "name", column = "File_name"),
            @Result(property = "no", column = "File_no"),
            @Result(property = "remarks", column = "File_remarks"),
            @Result(property = "school.id", column = "school_id"),
            @Result(property = "school.name", column = "school_name"),
            @Result(property = "school.no", column = "school_no"),
            @Result(property = "school.remarks", column = "school_remarks")
    })
    File findFileById(Integer id);
}